<%--
  Created by IntelliJ IDEA.
  User: 檬zhu
  Date: 2022/6/29
  Time: 18:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加店面</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/resources/layui/css/layui.css">
    <script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=Lbn1SZBh2NknQy3nUgWl0aspF84KXQma"></script>
</head>
<body>
<%
    if (request.getAttribute("addAddrSuccess") != null) {
        out.println("<script language='JavaScript'>alert('添加店面成功');window.location.href='/merchant/addAddressPage'</script>");
        return;
    }
%>
<form class="layui-form layui-form-pane" action="${pageContext.request.contextPath}/merchant/addAddress" method="post"
      style="margin-left:10%;">
    <div class="layui-form-item">
        <label class="layui-form-label">店面名称</label>
        <div class="layui-input-inline">
            <input type="text" name="name" lay-verify="title"
                   placeholder="请输入店面名称" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">店面地址</label>
        <div class="layui-input-inline" id="r-result">
            <input type="text" name="address" lay-verify="title" id="suggestId"
                   placeholder="请输入店面地址" autocomplete="off" class="layui-input">
        </div>
        <div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
    </div>
    <div id="l-map" style="height: 400px;width: 90%;margin-bottom: 10px"></div>

    <input type="hidden" name="lng" id="lng" value="">
    <input type="hidden" name="lat" id="lat" value="">
    <div class="layui-form-item">
        <div class="layui-input-block" style="margin-left: 0">
            <button type="submit" class="layui-btn" data-type="content" lay-submit="">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script src="../../resources/layui/layui.js"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form, layer = layui.layer, $ = layui.jquery;
        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 2) {
                    return '店面名称至少得2个字符';
                }
            }
        });
    });
</script>
</body>
</html>

<script type="text/javascript">
    // 百度地图API功能
    function G(id) {
        return document.getElementById(id);
    }

    var map = new BMap.Map("l-map");
    map.centerAndZoom("江门",12);                   // 初始化地图,设置城市和地图级别。

    map.enableScrollWheelZoom(true);
    var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
        {"input" : "suggestId"
            ,"location" : map
        });

    ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
        var str = "";
        var _value = e.fromitem.value;
        var value = "";
        if (e.fromitem.index > -1) {
            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        }
        str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

        value = "";
        if (e.toitem.index > -1) {
            _value = e.toitem.value;
            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        }
        str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
        G("searchResultPanel").innerHTML = str;
    });

    var myValue;
    ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
        var _value = e.item.value;
        myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

        setPlace();
    });

    function setPlace(){
        map.clearOverlays();    //清除地图上所有覆盖物
        function myFun(){
            var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
            document.getElementById("lng").value = pp.lng;
            document.getElementById("lat").value = pp.lat;
            map.centerAndZoom(pp, 18);
            map.addOverlay(new BMap.Marker(pp));    //添加标注
        }
        var local = new BMap.LocalSearch(map, { //智能搜索
            onSearchComplete: myFun
        });
        local.search(myValue);
    }
</script>
